#pragma once
#include <iostream>
#include <pthread.h>


#include "MyMutex.hpp"
#include "Log.hpp"


namespace CondValArea
{
    using namespace MyMutexArea;
    using namespace LogArea;
    class MyCondVal
    {
    public:
    MyCondVal()
    {
        int n = ::pthread_cond_init(&_cond,nullptr);
        if(n!=0)
        {
            LOG(LogLevel::ERROR)<<"pthread_cond_init error";
            exit(1);
        }
    }

    void PthreadWait(MyMutex& mutex)//等待
    {
        int n = ::pthread_cond_wait(&_cond,&mutex.GetMutex());
        if(n!=0)
        {
            LOG(LogLevel::ERROR)<<"pthread_cond_wait";
            exit(2);
        }
    }

    void PthreadSignal()//唤醒
    {
        int n = ::pthread_cond_signal(&_cond);
        if(n!=0)
        {
            LOG(LogLevel::ERROR)<<"pthread_cond_siganl error";
            exit(3);
        }
    }

    ~MyCondVal()
    {
        int n = ::pthread_cond_destroy(&_cond);
        if(n!=0)
        {
            LOG(LogLevel::ERROR)<<"pthread_cond_destroy error";
            exit(4);
        }
    }
    private:
        pthread_cond_t _cond;//条件变量
    };
}